<template>
  <div>
    <div style="margin-top: 15px;">
      <el-select slot="prepend" v-model="select" style="position: relative; left: 15px;" placeholder="筛选监控区域" clearable>
        <el-option v-for="opt in opts" :key="opt" :label="opt" :value="opt" />
        <!-- <el-option label="工地东区" value="工地东区" /> -->
      </el-select>
      <!-- <el-button type="primary" style="float: right; position: relative; right: 50px;" @click="updateMonitors">更新区域状态</el-button> -->
    </div>
    <div style="margin-left: 15px;">
      <el-row>
        <el-col v-for="monitor in monitors" :key="monitor.id" :span="5" style="margin-right: 4%;">
          <el-card v-if="select === monitor.area || select === ''" style="width: 100%; margin-top: 15px;" :body-style="{ padding: '0px' }" shadow="hover">
            <img src="../../assets/monitor/monitor-list.png" class="image" @click="$router.push('/monitor/monitor-realtime')">
            <div style="padding: 14px;">
              <span>{{ monitor.location }}</span>
              <span class="area">({{ monitor.area }})</span>
              <div class="bottom clearfix">
                <time class="time">{{ time }}</time>
                <el-button type="text" class="button" @click="$router.push('/monitor/monitor-playback')">监控回放</el-button>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      opts: ['工地东区', '工地南区', '工地西区', '工地北区'],
      select: '',
      monitors: [],
      time: ''
    }
  },
  mounted() {
    this.init()
  },
  beforeDestroy() {
    if (this.time) {
      clearInterval(this.getTimes)
    }
  },
  methods: {
    getMonitors() {
      this.axios.get('http://139.9.77.245:8081/monitor').then(res => {
        if (res.data.code === 0) {
          this.monitors = res.data.data
        } else if (res.data.code === 1) {
          this.$message.error('获取监控列表失败')
        } else {
          this.$message.error('获取监控列表失败')
        }
      })
    },
    getTimes() {
      setInterval(this.currentTime, 1000)
    },
    currentTime() {
      this.time = new Date().toLocaleString()
    },
    init() {
      this.getMonitors()
      this.time = new Date().toLocaleString()
      this.getTimes()
    },
    // updateMonitors() {
    //   this.monitors = []
    //   this.getMonitors()
    // }
  }

}
</script>

<style scoped>
.time {
    font-size: 13px;
    color: #999;
  }
  .area {
    font-size: 13px;
    color: #999;
  }

  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }

  .button {
    padding: 0;
    float: right;
  }

  .image {
    width: 100%;
    display: block;
    cursor: pointer;
  }

  .clearfix:before,
  .clearfix:after {
      display: table;
      content: "";
  }

  .clearfix:after {
      clear: both
  }

</style>
